//
//  MSNavigationBar.m
//  OrderingMerchantsSide
//
//  Created by iOS on 2018/9/20.
//  Copyright © 2018年 mls. All rights reserved.
//

#import "MSNavigationBar.h"

@interface MSNavigationBar ()

@property (nonatomic, copy) NSString        *title;     ///< 标题
@property (nonatomic)       MSNavBarType    navBarType;

@end

@implementation MSNavigationBar

#pragma mark - public method
- (void)resetNavBarWithTitle:(NSString *)title
                  navBarType:(MSNavBarType)navBarType
{
    self.title = title;
    self.navBarType = navBarType;
    switch (navBarType)
    {
        case MSNavBarTypeDefault:
        {
            self.backgroundColor = UIColor.whiteColor;
            self.leftBarButtonItem.hidden = NO;
            self.leftImageView.tintColor = MS_HEX_COLOR(0x121730);
            self.leftTextLabel.textColor = MS_HEX_COLOR(0x121730);
            self.titleLabel.textColor = MS_HEX_COLOR(0x121730);
        }
            break;
        case MSNavBarTypeClear:
        {
            self.backgroundColor = UIColor.clearColor;
            self.leftBarButtonItem.hidden = NO;
            self.leftImageView.tintColor = UIColor.whiteColor;
            self.leftTextLabel.textColor = UIColor.whiteColor;
            self.titleLabel.textColor = UIColor.whiteColor;
        }
            break;
        default:
            break;
    }
}


#pragma make - setter
- (void)setLeftImageView_size:(CGSize)leftImageView_size
{
    _leftImageView_size = leftImageView_size;
    ms_updateViewLayout(self);
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
}
- (void)setHasBackButton:(BOOL)hasBackButton
{
    _hasBackButton = hasBackButton;
    self.leftBarButtonItem.hidden = !self.hasBackButton;
    ms_updateViewLayout(self);
}


#pragma mark - event
/** 返回上一页 */
- (void)popLastPage
{
    if ([self.delegate respondsToSelector:@selector(popLastPage)])
    {
        [self.delegate popLastPage];
    }
}


#pragma mark - private method
// 判断一个点是否落在范围内
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL pointInSubViews = NO;
    for (UIView *view in self.subviews)
    {
        if (CGRectContainsPoint(view.frame, point))
        {
            pointInSubViews = YES;
            break;
        }
    }
    
    if (!pointInSubViews && self.navBarType == MSNavBarTypeClear)
    {
        return NO;
    }
    
    return [super pointInside:point
                    withEvent:event];
}


#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColor.clearColor;
        _navBarType = MSNavBarTypeDefault;
        _leftImageView_size = CGSizeMake(14, 21);
        [self addSubview:self.leftBarButtonItem];
        [self.leftBarButtonItem addSubview:self.leftImageView];
        [self.leftBarButtonItem addSubview:self.leftTextLabel];
        [self addSubview:self.titleLabel];
    }
    return self;
}
- (instancetype)initWithTitle:(NSString *)title
                hasBackButton:(BOOL)hasBackButton
{
    self = [super init];
    self.title = title;
    self.hasBackButton = hasBackButton;
    return self;
}


#pragma mark - UI
- (void)updateConstraints
{
    [super updateConstraints];
    [self.leftBarButtonItem mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.equalTo(self);
        make.height.mas_equalTo(44.0);
        make.width.mas_greaterThanOrEqualTo(65.0);
    }];
    [self.leftImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(7);
        make.size.mas_equalTo(self.leftImageView_size).priorityHigh();
        make.centerY.equalTo(self.leftBarButtonItem);
    }];
    [self.leftTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.leftImageView.mas_right).offset(3);
        make.right.mas_equalTo(-2);
        make.centerY.equalTo(self.leftBarButtonItem);
    }];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        if (self.leftBarButtonItem.isHidden)
        {
            make.left.mas_greaterThanOrEqualTo(55.0);
        }
        else
        {
            make.left.greaterThanOrEqualTo(self.leftBarButtonItem.mas_right).offset(8);
        }
        make.bottom.equalTo(self);
        make.height.mas_equalTo(44.0);
        make.centerX.equalTo(self).priorityHigh();
    }];
    [self.leftTextLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh
                                                        forAxis:UILayoutConstraintAxisHorizontal];
    [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
                                                     forAxis:UILayoutConstraintAxisHorizontal];
}


#pragma mark - lazy loading
- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(18);
        _titleLabel.textColor = MS_HEX_COLOR(0x121730);
    }
    
    return _titleLabel;
}
- (UIControl *)leftBarButtonItem
{
    if (!_leftBarButtonItem)
    {
        _leftBarButtonItem = [[UIControl alloc] init];
        [_leftBarButtonItem addTarget:self
                               action:@selector(popLastPage)
                     forControlEvents:UIControlEventTouchUpInside];
        _leftBarButtonItem.hidden = YES;
    }
    
    return _leftBarButtonItem;
}
- (UIImageView *)leftImageView
{
    if (!_leftImageView)
    {
        _leftImageView = [[UIImageView alloc] initWithImage:[MS_IMAGE_NAMED(@"back") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        _leftImageView.tintColor = MS_HEX_COLOR(0x121730);
    }
    
    return _leftImageView;
}

- (UILabel *)leftTextLabel
{
    if (!_leftTextLabel)
    {
        _leftTextLabel = [[UILabel alloc] init];
        _leftTextLabel.font = MS_SYSTEM_FONT_OF_SIZE(18);
        _leftTextLabel.textColor = MS_HEX_COLOR(0x121730);
    }
    
    return _leftTextLabel;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
